// pages/account/namecheck/personalcertify.js
var app = getApp();
var common = require('../../../common.js');
Page({
  data: {
    provinces: [],
    province: "",
    citys: [],
    city: "",
    countys: [],
    county: '',
    addrname:'',
    value: [0, 0, 0],
    values: [0, 0, 0],
    condition: false,
    regAddr: {
      provinces: [],
      province: "",
      citys: [],
      city: "",
      countys: [],
      county: '',
      value: [0, 0, 0],
      values: [0, 0, 0],
      condition: false,
    },
    busAddr: {
      provinces: [],
      province: "",
      citys: [],
      city: "",
      countys: [],
      county: '',
      value: [0, 0, 0],
      values: [0, 0, 0],
      condition: false,
    }
  },
  onLoad: function () {
    console.log("onLoad");
    var that = this;
    common.cRequst({
      url: 'http://' + common.serveIp + '/user.php?c=dp_city&a=city',
      success: function (res) {
        console.log(res);
        that.setData({
          cityData: JSON.parse(res.data),
          regAddr:{
            cityData:JSON.parse(res.data),
          },
          regAddr:{
            cityData:JSON.parse(res.data),
          },
        })
        var cityData = that.data.cityData;
        const provinces = [];
        const citys = [];
        const countys = [];
        for (let i = 0; i < cityData.length; i++) {
          provinces.push(cityData[i].name);
        }
        for (let i = 0; i < cityData[0].sub.length; i++) {
          citys.push(cityData[0].sub[i].name)
        }
        for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
          countys.push(cityData[0].sub[0].sub[i].name)
        }
        that.setData({
          'cityData':cityData,
          'regAddr': {
            'provinces': provinces,
            'citys': citys,
            'countys': countys,
            'province': cityData[0].name,
            'city': cityData[0].sub[0].name,
            'county': cityData[0].sub[0].sub[0].name,
            'value': [0, 0, 0],
            'values': [0, 0, 0],
            'cityData':cityData,
          },
          'busAddr': {
            'provinces': provinces,
            'citys': citys,
            'countys': countys,
            'province': cityData[0].name,
            'city': cityData[0].sub[0].name,
            'county': cityData[0].sub[0].sub[0].name,
            'value': [0, 0, 0],
            'values': [0, 0, 0],
            'cityData':cityData,
          }
        })
      }
    });
  },
  upimg: function (e) {
    var target = e.currentTarget;
    var dataset = target.dataset;
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        var obj = {};
        var filetype = res.tempFilePaths[0].substring(res.tempFilePaths[0].lastIndexOf('.'), res.tempFilePaths[0].length)
        wx.getStorage({
          key: 'user',
          success: function (data) {
            // success
            console.log(data)
            common.cRequst({
              url: 'http://' + common.serveIp + '/user.php?c=dp_entrust&a=uploadTransfer',
              data: {
                "session_id": data.data.session_id,
                "version": "app"
              },
              method: "Post",
              header: {
                'content-type': 'application/json'
              },
              success: function (d) {
                console.log(d);
                wx.uploadFile({
                  url: d.data.host, //仅为示例，非真实的接口地址
                  filePath: tempFilePaths[0],
                  name: 'file',
                  formData: {
                    "OSSAccessKeyId": d.data.accessid,
                    "policy": d.data.policy,
                    "signature": d.data.signature,
                    "key": d.data.dir + d.data.name + filetype,
                    "success_action_status": 200
                  },
                  success: function (b) {
                    var data = b.data;
                    //do something
                    var img = d.data.host + '/' + d.data.dir + d.data.name + filetype;
                    console.log(img)
                    obj[dataset['name']] = img

                    that.setData(obj)
                    wx.showToast({
                      title: '上传成功',
                      icon: 'success',
                      duration: 0
                    })
                  },
                  fail: function (res) {
                    wx.showToast({
                      title: '上传失败',
                      duration: 0
                    })
                  }
                })
              }
            })
          }
        })
      }
    })
  },
  formSubmit: function (e) {
    var regName = /^[\u4E00-\u9FA5]{2,4}$/;
    var regIdcard = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i;
    var regTel = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
    var regEmail = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
    e.detail.value.business_license_img = this.data.imgurl1;
    e.detail.value.idcard_front = this.data.imgurl2;
    e.detail.value.idcard_back = this.data.imgurl3;
    e.detail.value.tax_img = this.data.imgurl4;
    e.detail.value.tissue_img = this.data.imgurl5;
    e.detail.value.company_registry = this.data.regAddr.province + " " + this.data.regAddr.city + " " + this.data.regAddr.county;
    e.detail.value.contact_address = this.data.busAddr.province + " " + this.data.busAddr.city + " " + this.data.busAddr.county;
    console.log(e.detail.value);
    if (e.detail.value.company_name == "") {
      wx.showToast({
        title: '请输入公司名',
      })
    }
    else if (e.detail.value.company_registry_detailed == "") {
      wx.showToast({
        title: '请填写注册详细地址',
      })
    }
    else if (e.detail.value.business_license_no == "") {
      wx.showToast({
        title: '请填写营业执照编码',
      })
    }
    else if (e.detail.value.business_license_img == undefined) {
      wx.showToast({
        title: '请上传营业执照照片',
      })
    }
    else if (!regName.test(e.detail.value.legal_person)) {
      wx.showToast({
        title: '法人姓名格式错误',
      })
    }
    else if (!regIdcard.test(e.detail.value.idcard)) {
      wx.showToast({
        title: '法人身份证号码格式错误',
      })
    }
    else if (e.detail.value.idcard_front == undefined) {
      wx.showToast({
        title: '请上传身份证正面照',
      })
    }
    else if (e.detail.value.idcard_back == undefined) {
      wx.showToast({
        title: '请上传身份证反面照',
      })
    }
    else if (e.detail.value.tax_registration_no == "") {
      wx.showToast({
        title: '请填写税务登记证编号',
      })
    }
    else if (e.detail.value.tax_img == undefined) {
      wx.showToast({
        title: '请上传税务登记证照片',
      })
    }
    else if (e.detail.value.tissue_no == "") {
      wx.showToast({
        title: '请填写组织代码证编号',
      })
    }
    else if (e.detail.value.tissue_img == undefined) {
      wx.showToast({
        title: '请上组织代码证照片',
      })
    }
    else if (!regName.test(e.detail.value.contact_name)) {
      wx.showToast({
        title: '联系人姓名格式错误',
      })
    }
    else if (!regTel.test(e.detail.value.contact_tel)) {
      wx.showToast({
        title: '联系手机号格式错误',
      })
    }
    else if (e.detail.value.email !== "" && !regEmail.test(e.detail.value.email)) {
      wx.showToast({
        title: '邮箱格式错误',
      })
    }
    else if (e.detail.value.contact_address_info == "") {
      wx.showToast({
        title: '请填写详细经营地址',
      })
    }
    else {
      wx.getStorage({
        key: 'user',
        success: function (res) {
          common.cRequst({
            url: 'http://' + common.serveIp + '/user.php?c=dp_authentication&a=approve',
            data: {
              'type': 2,
              'session_id': res.data.session_id,
              'version': "app",
              'company_name':e.detail.value.company_name,
              'company_registry':e.detail.value.company_registry,
              'company_registry_detailed':e.detail.value.company_registry_detailed,
              'business_license_no':e.detail.value.business_license_no,
              'business_license_img':e.detail.value.business_license_img,
              'legal_person':e.detail.value.legal_person,
              'idcard': e.detail.value.idcard,
              'idcard_back': e.detail.value.idcard_back,
              'idcard_front': e.detail.value.idcard_front,
              'tax_registration_no': e.detail.value.tax_registration_no,
              'tax_img': e.detail.value.tax_img,
              'tissue_no': e.detail.value.tissue_no,
              'tissue_img': e.detail.value.tissue_img,
              'contact_name': e.detail.value.contact_name,
              'contact_address': e.detail.value.contact_address,
              'contact_tel': e.detail.value.contact_tel,
              'email': e.detail.value.email,
              'contact_address_info': e.detail.value.contact_address_info
            },
            method: "Post",
            success:function(res){
              wx.showToast({
                title:'提交成功'
              })
              wx.redirectTo({
                url: "/pages/account/namecheck/namecheck"
              })
            }
          })
        },
      })
    }
  },
  bindChange: function (e) {
    //console.log(e);
    var val = e.detail.value;
    var addrname = this.data.addrname;
    var crttarget = this.data[addrname];
    var t = crttarget.values;
    var tgtcityData = crttarget.cityData;
    tgtcityData = this.data.cityData;
    var obj = {};
    if (val[0] != t[0]) {
      const citys = [];
      const countys = [];
      for (let i = 0; i < tgtcityData[val[0]].sub.length; i++) {
        citys.push(tgtcityData[val[0]].sub[i].name)
      }
      for (let i = 0; i < tgtcityData[val[0]].sub[0].sub.length; i++) {
        countys.push(tgtcityData[val[0]].sub[0].sub[i].name)
      }
      crttarget.province = this.data[addrname].provinces[val[0]],
      crttarget.city = tgtcityData[val[0]].sub[0].name;
      crttarget.citys = citys;
      crttarget.county = tgtcityData[val[0]].sub[0].sub[0].name;
      crttarget.countys = countys;
      crttarget.values = val;
      crttarget.value = [val[0], 0, 0]
      obj[addrname] = crttarget;
      this.setData(obj)
    }
    if (val[1] != t[1]) {
      const countys = [];
      for (let i = 0; i < tgtcityData[val[0]].sub[val[1]].sub.length; i++) {
        countys.push(tgtcityData[val[0]].sub[val[1]].sub[i].name)
      }
      crttarget.city = this.data[addrname].citys[val[1]];
      crttarget.county = tgtcityData[val[0]].sub[val[1]].sub[0].name;
      crttarget.countys = countys;
      crttarget.values = val;
      crttarget.value = [val[0], val[1], 0];
      obj[addrname] = crttarget;
      this.setData(obj)
    }
    if (val[2] != t[2]) {
      crttarget.values = val;
      crttarget.value = [val[0], val[1], val[2]];
      crttarget.county = this.data[addrname].countys[val[2]];
      obj[addrname] = crttarget;
      this.setData(obj)
    }
  },
  open: function (e) {
    var addrname = e.currentTarget.dataset.name;
    var crttarget = this.data[addrname];
    crttarget.condition = !this.data[addrname].condition;
    var obj = {};
    obj[addrname] = crttarget;
    obj.addrname = addrname;
    this.setData(obj)
  }
})